\<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script type="text/javascript" src="lib/require.js"></script>

        <script type="text/javascript" src="lib/stats.js"></script>
        <script type="text/javascript" src="lib/dat.gui.js"></script>
    </head>
    <body style="margin:0px;">
        <canvas id="main"></canvas>
        <script>
            require(['../dist/claygl', 'js/AlchemyAOPass'], function(clay, AlchemyAOPass) {

                var config = {
                    kernelSize: 12,
                    bias: 0.05,
                    radius: 4,
                    scale: 1,
                    power: 1,
                    epsilon: 0.0001
                };

                var gBuffer = new clay.deferred.GBuffer();
                var renderer = new clay.Renderer({
                    canvas: document.getElementById('main'),
                    devicePixelRatio: 1.0
                });
                renderer.resize(window.innerWidth, window.innerHeight);
                gBuffer.resize(renderer.getWidth(), renderer.getHeight());

                var ssaoPass = new AlchemyAOPass({
                    gBuffer: gBuffer
                });

                var camera = new clay.camera.Perspective({
                    far: 10000,
                    aspect: renderer.getViewportAspect()
                });

                var scene = new clay.Scene();

                function randomInSquare(size) {
                    return (Math.random() - 0.5) * size * 2;
                }

                var cubeGeo = new clay.geometry.Cube();
                for (var i = 0; i < 20; i ++) {
                    for (var j = 0; j < 20; j ++) {
                        var mesh = new clay.Mesh({
                            material: new clay.Material({
                                shader: clay.shader.library.get('clay.standard')
                            }),
                            geometry: cubeGeo
                        });
                        mesh.position.set(
                            randomInSquare(20), randomInSquare(20), randomInSquare(20)
                        );
                        var scale = Math.random() * 5 + 5;
                        mesh.scale.set(scale, scale, scale);
                        scene.add(mesh);
                    }
                }
                scene.add(new clay.light.Ambient({
                    intensity: 5
                }));

                camera.position.set(0, 80, 150);
                camera.lookAt(scene.position);

                var control = new clay.plugin.OrbitControl({
                    domElement: renderer.canvas,
                    target: camera
                });

                var timeline =  new clay.Timeline();

                timeline.on('frame', function (deltaTime) {
                    control.update(deltaTime);
                    stats.update();

                    renderer.render(scene, camera);
                    scene.update();
                    camera.update();
                    gBuffer.update(renderer, scene, camera);
                    // gBuffer.renderDebug(renderer, camera, 'depth');
                    ssaoPass.render(renderer, camera);
                });
                timeline.start();

                var stats = new Stats();
                stats.domElement.style.position = 'absolute';
                stats.domElement.style.top = '0px';
                stats.domElement.style.left = '0px';
                document.body.appendChild(stats.domElement);

                var gui = new dat.GUI();

                var kernelCanvas = document.createElement('canvas');
                kernelCanvas.width = kernelCanvas.height = 100;
                var kernelCtx = kernelCanvas.getContext('2d');
                kernelCanvas.style.cssText = 'position:absolute;left:0;bottom:0;z-index:100;';
                document.body.appendChild(kernelCanvas);

                function updateConfig() {
                    for (var name in config) {
                        ssaoPass.setParameter(name, config[name]);
                    }
                    kernelCtx.clearRect(0, 0, 100, 100);
                    var kernel = ssaoPass._ssaoPass.getUniform('kernel');

                    kernelCtx.beginPath();
                    for (var i = 0; i < kernel.length;) {
                        var x = kernel[i++];
                        var y = kernel[i++];
                        kernelCtx.rect(x * 50 + 50, y * 50 + 50, 2, 2);
                    }
                    kernelCtx.fill();
                }

                gui.add(config, 'kernelSize', 4, 128).step(1).onChange(updateConfig);
                gui.add(config, 'radius', 0, 10).onChange(updateConfig);
                gui.add(config, 'scale', 0, 2).onChange(updateConfig);
                gui.add(config, 'power', 0.1, 6).onChange(updateConfig);
                gui.add(config, 'bias').onChange(updateConfig);
                gui.add(config, 'epsilon').onChange(updateConfig);

                updateConfig();
            });
        </script>
    </body>
</html>